package com.lagou.homework.format;

import com.lagou.homework.config.PropertiesConfiguration;
import com.lagou.homework.module.Word;
import lombok.SneakyThrows;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordSortOutputFormat extends FileOutputFormat<Word, NullWritable> {

    private static final String OUTPUT_PATH = PropertiesConfiguration.getProperties("homework.path.output");

    @SneakyThrows
    @Override
    public RecordWriter<Word, NullWritable> getRecordWriter(TaskAttemptContext context) {
        Configuration conf = context.getConfiguration();
        FileSystem fs = FileSystem.get(conf);
        FSDataOutputStream os = fs.create(new Path(OUTPUT_PATH +"/word"));
        return new WordSortWriter(os);
    }
}
